<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <div style="margin-bottom: -20px">
      <el-form :model="searchObj" :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchObj.deviceName" placeholder="设备名称" />
        </el-form-item>
        <el-form-item>
          <el-input v-model.number="searchObj.minPrice" placeholder="最低价格" />
        </el-form-item>
        <el-form-item>
          <el-input v-model.number="searchObj.maxPrice" placeholder="最高价格" />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
        <el-button type="default" icon="el-icon-refresh" @click="reset()">清空</el-button>
      </el-form>
    </div>

    <div class="el-toolbar">
      <div class="el-toolbar-body" style="justify-content: flex-start; margin-top: 10px">
        <el-button type="primary" icon="el-icon-plus" @click="openEdit(null)">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="batchDelete">删除</el-button>
        <el-button type="primary" icon="el-icon-download" @click="exportData">导出</el-button>
        <el-button type="primary" icon="el-icon-upload2" @click="importData">导入</el-button>
      </div>
    </div>

    <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
    <el-table
      :data="data.list"
      :border="true"
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" fixed="left" width="55" />
      <el-table-column type="index" width="80" fixed="left" label="序号" />
      <el-table-column prop="labName" min-width="180" fixed="left" label="实验室分室名称" />
      <el-table-column prop="deviceName" label="设备名称" />
      <el-table-column prop="deviceDate" min-width="160" label="购买时间" />
      <el-table-column prop="devicePrice" label="设备价格" />
      <el-table-column prop="guarantee" label="保修时间" />
      <el-table-column prop="deviceFactory" min-width="200" label="生产厂商" />
      <el-table-column prop="createAt" label="创建人" />
      <el-table-column prop="createTime" min-width="160" label="创建时间" />
      <el-table-column prop="updateAt" min-width="100" label="最后修改人" />
      <el-table-column prop="updateTime" min-width="160" label="修改时间" />

      <!-- 操作 -->
      <el-table-column label="操作" fixed="right" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="remove(scope.row.id)"></el-button>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="openEdit(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        @current-change="getAll"
        @size-change="handleSizeChange"
        :current-page="pIndex"
        :page-size="pSize"
        :page-sizes="[3, 6, 9]"
        background
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="data.total"
      >
      </el-pagination>
    </div>

    <!-- 表单 -->
    <el-dialog :title="device.id ? '设备修改' : '设备新增'" :visible.sync="visible" :before-close="close">
      <el-form ref="deviceForm" :model="device" :rules="rules" label-width="120px">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="ID" prop="id" :hidden="true">
              <el-input v-model="device.id" disabled />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="所属实验分室:" prop="labId">
              <el-select v-model="device.labId" placeholder="请选择实验分室" style="width: 100%">
                <el-option v-for="item in labSub" :key="item.id" :label="item.labName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="设备名称:" prop="deviceName">
              <el-input v-model.trim="device.deviceName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="购买时间:" prop="deviceDate">
              <div class="block">
                <el-date-picker
                  v-model.trim="device.deviceDate"
                  style="width: 100%"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="date"
                  placeholder="请输入购买时间"
                  align="left"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="设备价格:" prop="devicePrice">
              <el-input v-model.number="device.devicePrice" placeholder="请输入设备价格" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="保修时间:" prop="guarantee">
              <el-select v-model="device.guarantee" placeholder="请选择保修时间" style="width: 100%">
                <el-option value="一年">一年</el-option>
                <el-option value="二年">二年</el-option>
                <el-option value="三年">三年</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
            <el-form-item label="生产厂商:" prop="deviceFactory">
              <el-input v-model.trim="device.deviceFactory" placeholder="请输入生产厂商" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button @click="resetForm">重 置</el-button>
        <el-button type="primary" @click="save('deviceForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入api接口定义的js文件
import deviceApi from '@/api/device'
import labSubApi from '@/api/labSub'

export default {
  data() {
    // 定义变量和初始值
    return {
      // 列表相关
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      searchObj: {}, // 条件封装对象
      data: [], // 数据
      loading: false,
      ids: [],

      // 表单相关
      visible: false,
      device: {
        id: undefined,
        deviceName: '',
        deviceDate: '',
        devicePrice: '',
        guarantee: '',
        deviceFactory: '',
        labId: ''
      },
      labSub: [],
      rules: {
        deviceName: [{ required: true, message: '设备名称不能为空', trigger: 'blur' }],
        deviceDate: [{ required: true, message: '请选择购买时间', trigger: 'change' }],
        devicePrice: [{ required: true, message: '设备价格不能为空', trigger: 'blur' }],
        guarantee: [{ required: true, message: '请选择保修时间', trigger: 'change' }],
        deviceFactory: [{ required: true, message: '生产厂商不能为空', trigger: 'blur' }],
        labId: [{ required: true, message: '请选择实验室', trigger: 'change' }]
      }
    }
  },
  created() {
    //勾子函数，在页面渲染之前执行，一般调用methods定义的方法得到数据
    this.getAll() //调用methods中的函数
  },
  methods: {
    //实验室课程信息
    getAll(current = 1) {
      this.loading = true
      // 加载列表数据
      this.pIndex = current //添加当前页参数
      // axios异步获取远程数据（ajax），通过api
      deviceApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          //请求成功response是接口返回数据
          //返回集合赋值list
          this.data = response.data
          this.loading = false
        })
        .catch((error) => {
          this.loading = false
          //请求失败
          console.log(error)
        })
    },

    handleSizeChange(pageSize) {
      // 改变页码
      this.pSize = pageSize
      //重新调用数据加载方法 --> 方法中已经加入的查询参数
      this.getAll()
    },
    remove(id) {
      //删除实验室设备信息
      this.$confirm('此操作将永久删除实验室设备信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //确定执行then方法
        //调用接口
        deviceApi.remove(id).then((resp) => {
          this.$message.success('删除成功!')
          this.getAll()
        })
      })
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 批量删除
    batchDelete() {
      if (this.ids.length > 0) {
        this.$confirm('此操作将永久删除实验室信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 调用接口
          deviceApi.batchRemove(this.ids).then((resp) => {
            // 提示
            this.$message.success('删除成功!')
            // 刷新页面
            this.getAll()
          })
        })
      } else {
        this.$message.warning('请至少选择一条数据')
      }
    },

    // 打开表单
    openEdit(id) {
      if (id != null) {
        this.getById(id)
      }

      this.getLabSub()

      this.visible = true
    },

    // 查询实验室
    getLabSub() {
      labSubApi.getList().then((res) => {
        this.labSub = res.data
      })
    },

    // 根据Id查询
    getById(id) {
      deviceApi.getById(id).then((res) => {
        this.device = res.data
      })
    },

    // 关闭表单
    close() {
      this.visible = false
      this.$refs['deviceForm'].resetFields()
    },

    // 重置表单
    resetForm() {
      if (this.device.id) {
        this.getById(this.device.id)
      }

      this.$refs['deviceForm'].resetFields()
    },

    // 保存
    save(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const apiCall = this.device.id ? deviceApi.update(this.device) : deviceApi.save(this.device)
          apiCall.then(() => {
            this.$message.success('操作成功')
            this.close()
            this.getAll()
          })
        }
      })
    },

    // 清空
    reset() {
      this.searchObj = {}
      this.getAll()
    },

    // 导出
    exportData() {
      deviceApi.exportData().then((res) => {
        this.$message.success(res.data)
      })
    },

    // 导入
    importData() {
      deviceApi.importData().then((res) => {
        this.$message.success(res.data)
        this.getAll()
      })
    }
  }
}
</script>

<style></style>
